define([
    "dojo/_base/declare",
    "gistech/widgets/normalDialog/NormalDialog",
    "dojo/text!./templates/Template.html",
    "dojo/_base/lang",
    "dojo/dom-style",
    "dojo/dom-construct",
    "dojo/on","dojo/topic",
    "dojo/_base/connect",
    "gistech/util/ajaxUtil",
    "esri/geometry/geodesicUtils",
    "esri/geometry/webMercatorUtils",
    "gistech/widgets/drawGeometryWidget/DrawGeometryWidget",
    "esri/layers/FeatureLayer","esri/InfoTemplate",
    "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",
    "esri/renderers/UniqueValueRenderer", "esri/Color",
    "dojo/dom-style","dojo/query","dojo/dom-class"],
    function(declare,NormalDialog,template,lang,style,domConstruct,
    		on,topic,eventconnect,ajaxUtil,geodesicUtils,webMercatorUtils,DrawGeometryWidget,FeatureLayer,
    		InfoTemplate,SimpleLineSymbol,SimpleFillSymbol,UniqueValueRenderer,Color,
    		domStyle,query,domClass){
        return declare([NormalDialog],{
            templateString:template,
            projectType:"核查项目",
            LandFeatureLayer:null,
            landgeo:null,
            handles:[],
            landinfoTemplate:null,
            allowGet:null,
            constructor:function (args) {
            	//debugger;
            	this.inherited(arguments);
            	this.map = args.map;
            },
            postCreate:function(){
                this.inherited(arguments);
                on(this.save, "click",lang.hitch(this,this.saveLandGeometryToDB));
                on(this.getLandInfo,"click",lang.hitch(this,this.allowGetLandInfo));
                on(this.cancle,"click",lang.hitch(this,this.closeDialog));
                on(this.drowgraphic,"click",lang.hitch(this,this.createDrawbar));
            },
            startup:function(){
                this.inherited(arguments);    
                this.init();
                this.addEvents();
            },
            addEvents:function(){
            	debugger;
            	on(this.LandFeatureLayer,"edits-complete",lang.hitch(this,function(){
            		debugger;
            		this.LandFeatureLayer.refresh();
            	}));
            },
            init:function(){
            	 this.allowGet = false;
            	 this.addLandsFeatureLayer();
            	 dojo.query("#XH")[0].value = "L"+getDateStr();
            	 dojo.query("#REGISTERDATE")[0].value = reduceformatDate(0,"-");
            	 var projecttype = dojo.query(".projecttype input");
                 for(var i=0;i<projecttype.length;i++){
     				on(projecttype[i], "click",lang.hitch(this,this.changeProjectTypeHandler));}
                 
                 //初始化信息 
                 if(ioc.map.userInfoData.DISPLAYNAME !=undefined && ioc.map.userInfoData.DISPLAYNAME !=''){
                	 dojo.byId('REGISTERUSER').value = ioc.map.userInfoData.DISPLAYNAME;
                 }
                 if(ioc.map.userInfoData.ORGANIZATIONCOMPANYNAME !=undefined && ioc.map.userInfoData.ORGANIZATIONCOMPANYNAME !=''){
                	 dojo.byId('landCompany').value = ioc.map.userInfoData.ORGANIZATIONCOMPANYNAME;
                 }
                 if(ioc.map.userInfoData.ORGANIZATIONBUSINESSNAME !=undefined && ioc.map.userInfoData.ORGANIZATIONBUSINESSNAME !=''){
                	 dojo.byId('landBusiness').value = ioc.map.userInfoData.ORGANIZATIONBUSINESSNAME;
                 }
                 if(ioc.map.userInfoData.ORGANIZATIONDEVELOPNAME !=undefined && ioc.map.userInfoData.ORGANIZATIONDEVELOPNAME !=''){
                	 dojo.byId('landDevelop').value = ioc.map.userInfoData.ORGANIZATIONDEVELOPNAME;
                 }
                 topic.subscribe("MapLandClickHandler",lang.hitch(this,this.listenerMapLandClickHandler));
//               topic.subscribe("Drawed",lang.hitch(this,this.fullLandExpandAttribute));
                 topic.subscribe("landExpandManagereditGeoed",lang.hitch(this,this.fullLandExpandAttribute));
                 topic.subscribe("addLandFeatureLayer",lang.hitch(this,this.addLandsFeatureLayer));
                 handles=[on(this.LandFeatureLayer ,"click", lang.hitch(this,function(evt) {
             		topic.publish("MapLandClickHandler",evt);
             	}))]; 
            },
            //从绘制的图形中获取面积信息
            fullLandExpandAttribute:function(args){
            	debugger
            	this.landgeo = args.geo;
            	this.refreshLandGeometry(this.landgeo); 	
            },
            //从绘制的图形上获取面积
            refreshLandGeometry:function(args){
            	var geo = webMercatorUtils.webMercatorToGeographic(args.geometry);
            	var area=Math.abs(geodesicUtils.geodesicAreas([geo], esri.Units.SQUARE_METERS));
    			dojo.byId("TOTALAREA").value = area.toFixed(3);
    			dojo.byId("NETLANDAREA").value = area.toFixed(3);
            },
            allowGetLandInfo:function(evt){
            	debugger;
            	var item = evt.currentTarget;
            	debugger;
            	this.allowGet = item.checked;
            	style.set(dojo.byId("alertinfo"),"display",this.allowGet?"block":"none");
            },
            createDrawbar:function(){
            	debugger;
            	if(dijit.byId("LandExpandDraw")){
            		return false;
            	}else{
            		debugger
            		Global.currentDraw="landExpandManager";
            		var drawGeometryWidget = new DrawGeometryWidget({map:this.map,id:"LandExpandDraw"});
                	drawGeometryWidget.startup();
            	}},
            addLandsFeatureLayer:function (){
            	this.LandFeatureLayer=new FeatureLayer(mapinfoTemplate.LandFeatureLayerEdit.layerurl);
            },
            listenerMapLandClickHandler:function(evt){
            	var layer = evt.currentTarget;//点击的要素所在图层
            	var layerid = layer.id.split("_")[0];//"LandFeatureLayer_layer"
            	if(this.allowGet){
            		var attrs = evt.graphic.attributes;
            		var fieldmap = mapinfoTemplate[layerid].fieldmap;
            		if(fieldmap.hasOwnProperty('LANDNAME')){
            			dojo.query("#LANDNAME")[0].value = attrs[fieldmap["LANDNAME"]];     
            		}
            		if(fieldmap.hasOwnProperty('LANDBH')){
            			dojo.query("#LANDBH")[0].value = attrs[fieldmap["LANDBH"]];     
            		}
            		if(fieldmap.hasOwnProperty('CITY')){ 
            			dojo.query("#CITY")[0].value = attrs[fieldmap["CITY"]]; 
            		}
            		if(fieldmap.hasOwnProperty('DISTRICT')){ 
            			dojo.query("#DISTRICT")[0].value = attrs[fieldmap["DISTRICT"]];  
            		}
            		if(fieldmap.hasOwnProperty('ZHENJIE')){ 
            			dojo.query("#ZHENJIE")[0].value = attrs[fieldmap["ZHENJIE"]]; 
            		}
            		if(fieldmap.hasOwnProperty('ADDRESS')){ 
            			dojo.query("#ADDRESS")[0].value = attrs[fieldmap["ADDRESS"]];  
            		}
            		if(fieldmap.hasOwnProperty('ADDRESS')){ 
            			dojo.query("#ADDRESS")[0].value = attrs[fieldmap["ADDRESS"]];  
            		}
            		if(fieldmap.hasOwnProperty('PROJECTTYPE')){ 
            			this.getIndexFromSelectListByVaue("PROJECTTYPE",attrs[fieldmap["PROJECTTYPE"]]);	
            		}
            		if(fieldmap.hasOwnProperty('EXPENDTYPE')){ 
            			this.getIndexFromSelectListByVaue("EXPENDTYPE",attrs[fieldmap["EXPENDTYPE"]]);
            		}
            		if(fieldmap.hasOwnProperty('TGXX')){ 
            			this.getIndexFromSelectListByVaue("TGXX",attrs[fieldmap["TGXX"]]);
            		}
            		if(fieldmap.hasOwnProperty('KGXX')){ 
            			this.getIndexFromSelectListByVaue("KGXX",attrs[fieldmap["KGXX"]]);
            		}
            		if(fieldmap.hasOwnProperty('XZJSXX')){ 
            			this.getIndexFromSelectListByVaue("XZJSXX",attrs[fieldmap["XZJSXX"]]);
            		}
            		if(fieldmap.hasOwnProperty('VOLUMERATIO')){ 
            			dojo.query("#VOLUMERATIO")[0].value = attrs[fieldmap["VOLUMERATIO"]];  
            		}
            		if(fieldmap.hasOwnProperty('OWNERSHIP')){ 
            			this.getIndexFromSelectListByVaue("OWNERSHIP",attrs[fieldmap["OWNERSHIP"]]);
            		}
            		if(fieldmap.hasOwnProperty('COMPANY')){ 
            			dojo.query("#COMPANY")[0].value =  attrs[fieldmap["COMPANY"]];     
            		}
            		if(fieldmap.hasOwnProperty('LICENCE')){ 
            			dojo.query("#LICENCE")[0].value = attrs[fieldmap["LICENCE"]];       
            		}
            		if(fieldmap.hasOwnProperty('QSR')){ 
            			this.getIndexFromSelectListByVaue("QSR",attrs[fieldmap["QSR"]]);
            		}
            		if(fieldmap.hasOwnProperty('QSRNAME')){ 
            			dojo.query("#QSRNAME")[0].value = attrs[fieldmap["QSRNAME"]];       
            		}
            		if(fieldmap.hasOwnProperty('TOTALAREA')){ 
            			dojo.query("#TOTALAREA")[0].value = attrs[fieldmap["TOTALAREA"]];      
            		}
            		if(fieldmap.hasOwnProperty('NETLANDAREA')){ 
            			dojo.query("#NETLANDAREA")[0].value = attrs[fieldmap["NETLANDAREA"]];      
            		}
            		if(fieldmap.hasOwnProperty('TELL')){ 
            			dojo.query("#TELL")[0].value = attrs[fieldmap["TELL"]];      
            		}
            		if(fieldmap.hasOwnProperty('BZJ')){ 
            			dojo.query("#BZJ")[0].value = attrs[fieldmap["BZJ"]];   
            		}
            		if(fieldmap.hasOwnProperty('LANDVALUE')){ 
            			dojo.query("#LANDVALUE")[0].value = attrs[fieldmap["LANDVALUE"]];    
            		}
            		if(fieldmap.hasOwnProperty('YJQYSJ')){ 
            			dojo.query("#YJQYSJ")[0].value = attrs[fieldmap["YJQYSJ"]];  
            		}
            		if(fieldmap.hasOwnProperty('SJQYSJ')){ 
            			dojo.query("#SJQYSJ")[0].value = attrs[fieldmap["SJQYSJ"]]; 
            		}
            		if(fieldmap.hasOwnProperty('BZ')){ 
            			dojo.query("#BZ")[0].value = attrs[fieldmap["BZ"]] ;
            		}
            	}else{
            		return;
            	}
            },
            getIndexFromSelectListByVaue:function(selectId,checkValue){
            	debugger
            	var select = dojo.byId(selectId);  
                for(var i=0; i<select.options.length; i++){  
                    if(select.options[i].innerHTML == checkValue){  
                        select.options[i].selected = true;  
                        break;  
                    }  
                } 
                return;
            },
            closeDialog:function(){
            	this.allowGet=false;
    			this.inherited(arguments);
    			this.destroy();
    			topic.publish("LandExpandManageWidgetDestroy");
    		},
    		changeProjectTypeHandler:function(item){
    			var selectitem = item.currentTarget;
    			this.projectType = selectitem.attributes["value"].value;
    			var isshow = false;
    			debugger
    			if(this.projectType=="核查项目"){
    				isshow = true;
    			}else if(this.projectType=="洽谈项目"){
    				isshow = false;
    				dojo.byId("YJQYSJ_DESC").innerHTML = "预计签约：";
    				dojo.byId("SJQYSJ_DESC").innerHTML = "预计获取：";
    			}else{
    				isshow = false;
    				dojo.byId("YJQYSJ_DESC").innerHTML = "实际签约：";
    				dojo.byId("SJQYSJ_DESC").innerHTML = "实际获取：";
    			}
    			dojo.toggleClass(dojo.byId("QYSJ"),"unshowed",isshow);
    		},
            saveLandGeometryToDB:function(){
            	debugger
            	var validFields={
					VOLUMERATIO:dojo.query("#VOLUMERATIO")[0],
					BZJ:dojo.query("#BZJ")[0],          
					LANDVALUE:dojo.query("#LANDVALUE")[0]
            	}
            	var reg=new RegExp("^[0-9]+\.{0,1}[0-9]{0,2}$");
            	var count=0
            	for(var i in validFields){
            		if(reg.exec(validFields[i].value)==null){
            			if(count++ == 0){
            				validFields[i].focus();
            			}
            			style.set(validFields[i],"border-color","red");
            		}else{
            			if(style.get(validFields[i],"border-color") == "rgb(255, 0, 0)"){
            				style.set(validFields[i],"border-color","");
            			}
            		}
            	}
            	if(count!=0){
            		return false;
            	}
				this.landgeo.attributes={
						XH:dojo.query("#XH")[0].value,           
						TYPE:this.projectType,         
						LANDBH:dojo.query("#LANDBH")[0].value,       
						LANDNAME:dojo.query("#LANDNAME")[0].value,     
						CITY:dojo.query("#CITY")[0].value,         
						DISTRICT:dojo.query("#DISTRICT")[0].value,     
						ZHENJIE:dojo.query("#ZHENJIE")[0].value,      
						ADDRESS:dojo.query("#ADDRESS")[0].value,      
						PROJECTTYPE:dojo.query("#PROJECTTYPE")[0].value,  
						EXPENDTYPE:dojo.query("#EXPENDTYPE")[0].value,   
						TGXX:dojo.query("#TGXX")[0].value,         
						KGXX:dojo.query("#KGXX")[0].value,         
						XZJSXX:dojo.query("#XZJSXX")[0].value,       
						TOTALAREA:dojo.query("#TOTALAREA")[0].value,    
						NETLANDAREA:dojo.query("#NETLANDAREA")[0].value,  
						VOLUMERATIO:dojo.query("#VOLUMERATIO")[0].value, 
						OWNERSHIP:dojo.query("#OWNERSHIP")[0].value,    
						COMPANY:dojo.query("#COMPANY")[0].value,      
						LICENCE:dojo.query("#LICENCE")[0].value,      
						QSR:dojo.query("#QSR")[0].value,          
						QSRNAME:dojo.query("#QSRNAME")[0].value,      
						TELL:dojo.query("#TELL")[0].value,         
						BZJ:dojo.query("#BZJ")[0].value,          
						LANDVALUE:dojo.query("#LANDVALUE")[0].value,    
						REGISTERDATE:dojo.query("#REGISTERDATE")[0].value, 
						REGISTERUSER:ioc.map.userInfoData.USERNAME,						
						YJQYSJ:dojo.query("#YJQYSJ")[0].value,       
						SJQYSJ:dojo.query("#SJQYSJ")[0].value,       
						BZ:dojo.query("#BZ")[0].value 
				};
				var self=this
				this.LandFeatureLayer.applyEdits([this.landgeo],null,null,
						function(evt){layer.close(index) ;self.closeDialog(),layer.msg("保存成功!",{icon: 1,time: 3000})},
						function(evt){layer.close(index) ;layer.msg("保存失败!",{icon: 2,time: 3000})});var index = layer.load(1);},
    });
});

